/*
 * UT_runner.c
 *
 *  Created on: 2010-04-01
 *      Author: Godwinguo
 */

#include "ut_features.h"
#include "ut_runner.h"


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

/*************************************************************************
* LOCAL FUNCTION
*************************************************************************/

static ut_result_t __ut_test_group( ut_group_ptr_t group );

/*************************************************************************
* IMPLEMENTATION
*************************************************************************/

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
void ut_coloring( unsigned long color )
{

}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
void ut_trace( char * fmt, ... )
{
	static unsigned long	written;
	static unsigned long	size;
	static char				text[1024];
	va_list					ap = NULL;
	
	va_start( ap, fmt );
	
	vsprintf( &text[0], fmt, ap );
	size = strlen( &text[0] );

	printf( text );
	printf( "\n" );
	va_end( ap );
 
	
	

}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
static ut_result_t __ut_test_group( ut_group_ptr_t group )
{
	unsigned long	i;
	unsigned long	ret_val;
	ut_result_t		result = { 0 };
	ut_case_ptr_t	curr_case;

	ut_coloring( UT_FG_DEFAULT );
	ut_trace( "[__GROUP___] Testing '%s' ...", group->name );

	for ( i = 0; ; ++i )
	{
		curr_case = &group->cases[i];
		if ( 0 == curr_case->do_test )
			break;

		ret_val = curr_case->do_test();
		if ( UT_RESULT_PASS == ret_val )
		{
			ut_coloring( UT_FG_GREEN );
			ut_trace( "[  PASSED  ] Case '%s'", curr_case->name );

			result.pass += 1;
		}
		else if ( UT_RESULT_FAIL == ret_val )
		{
			ut_coloring( UT_FG_RED );
			ut_trace( "[  FAILED  ] Case '%s'", curr_case->name );

			result.fail += 1;
		}
		else
		{
			ut_coloring( UT_FG_DEFAULT );
			ut_trace( "[  SKIPED  ] Case '%s'", curr_case->name );

			result.skip += 1;
		}
	}

	if ( result.fail > 0 )
	{
		ut_coloring( UT_FG_RED );
	}
	else
	{
		ut_coloring( UT_FG_DEFAULT );
	}

	ut_trace( "[__GROUP___] ======== %d failed case(s) ========", result.fail );

	return result;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
void ut_test_groups( ut_group_ptr_t pgroups, unsigned long ngroups )
{
	unsigned long	i;
	ut_group_ptr_t	curr_grp;
	ut_result_t		total_result = { 0 };
	ut_result_t		group_result = { 0 };

	ut_coloring( UT_FG_DEFAULT );
	ut_trace( "" );
	ut_trace( "[==SUITE===] Running %d test-case group(s) ...", ngroups );

	for ( i = 0; i < ngroups; ++i )
	{
		curr_grp = &pgroups[i];
		if ( 0 == curr_grp->cases )
			break;

		group_result = __ut_test_group( curr_grp );

		total_result.pass += group_result.pass;
		total_result.fail += group_result.fail;
		total_result.skip += group_result.skip;
	}

	ut_coloring( UT_FG_DEFAULT );
	ut_trace( "[==SUITE===] PASS [%d], FAIL [%d], SKIP [%d]", total_result.pass, total_result.fail, total_result.skip );
	ut_trace( "" );
}
